package leetcode;

import java.util.HashMap;
import java.util.Map;

public class l0219_存在重复元素_II {

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        //key为元素本身，value为元素在数组nums的下标索引
        Map<Integer, Integer> dataMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer value = dataMap.get(nums[i]);
            if (value == null) {
                dataMap.put(nums[i], i);
                continue;
            }
            if (i - value <= k) {
                return true;
            }
            dataMap.put(nums[i], i);
        }
        return false;
    }

}
